package com.yunxi.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.hubToBiz.pojo.CancelVOAPP;
import com.yunxi.order.pojo.OrderCustomerExOrderOemSaveReqVO;
import com.yunxi.order.pojo.OrderCustomerVO;
import com.yunxi.order.service.OrderCustomerDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 零售订单 前端控制器
 * </p>
 *
 * @author
 * @since 2019-05-29
 */
@RestController
@RequestMapping("/admin/orderCustomer")
@Api(tags = "pms:orderCustomer", description = "零售订单接口")
public class OrderCustomerController {
   private static final Logger logger = LoggerFactory.getLogger(OrderCustomerController.class);

   @Autowired
   OrderCustomerDubboService orderCustomerDubboService;

   @GetMapping("/page")
   @ApiOperation(value = "查询零售订单分页")
   public ApiResult page(OrderCustomerVO orderCustomerVO, Integer pageNum, Integer pageSize, @CurrentUser LoginInfo loginInfo) throws Exception {
      return ResponseUtil.ok(orderCustomerDubboService.findPageByVO(orderCustomerVO, pageNum, pageSize, loginInfo));
   }

   @GetMapping("/list")
   @ApiOperation(value = "查询零售订单列表")
   public ApiResult list(OrderCustomerVO v, @CurrentUser LoginInfo loginInfo) throws Exception {
      if (loginInfo == null) return ResponseUtil.fail(-1, "请登录");
      return ResponseUtil.ok(orderCustomerDubboService.listByVo(v, loginInfo));
   }

   @GetMapping("/load")
   @ApiOperation(value = "读取零售订单单个")
   public ApiResult load(String id, @CurrentUser LoginInfo loginInfo) throws Exception {
      return ResponseUtil.ok(orderCustomerDubboService.getById(id));
   }

   @PostMapping("/save")
   @ApiOperation(value = "保存零售订单信息")
   public ApiResult save(@RequestBody OrderCustomerVO orderCustomerVO, @CurrentUser LoginInfo loginInfo) throws Exception {

      if (loginInfo.getPartner() != null && StringUtils.isEmpty(orderCustomerVO.getPartnerCode())) {
         PartnerVO partnerVO = JSONObject.parseObject(JSONObject.toJSONString(loginInfo.getPartner()), PartnerVO.class);
         orderCustomerVO.setPartnerCode(partnerVO.getPartnerCode());
      }
      if(StringUtils.isEmpty(orderCustomerVO.getPartnerCode()) ) return ResponseUtil.fail(4001,"厂端无法创建零售订单");
      if(StringUtils.isEmpty(orderCustomerVO.getProductPackageId())) return ResponseUtil.fail(4001,"未选择车辆,无法创建零售订单");
      if (StringUtils.isEmpty(orderCustomerVO.getOrderCustomerId())) {
         return orderCustomerDubboService.save(orderCustomerVO, loginInfo);
      } else {
         return orderCustomerDubboService.update(orderCustomerVO, loginInfo);
      }
   }

   @PostMapping("/assign")
   @ApiOperation(value = "零售订单批量分配销售顾问")
   public ApiResult updateBatch(@RequestBody List<OrderCustomerVO> orderCustomerVOList, @CurrentUser LoginInfo loginInfo) throws Exception {
      orderCustomerDubboService.updateBatch(orderCustomerVOList);
      return ResponseUtil.ok("success");
   }

   @PostMapping("/assignPartner")
   @ApiOperation(value = "零售订单批量分配渠道商")
   @Deprecated //未被调用过
   public ApiResult assignPartner(@RequestBody List<OrderCustomerVO> orderCustomerVOList, @CurrentUser LoginInfo loginInfo) throws Exception {
      return orderCustomerDubboService.assignPartner(orderCustomerVOList, loginInfo);
   }

   @PostMapping("/del")
   @ApiOperation(value = "删除")
   public ApiResult del(@RequestBody List<OrderCustomerVO> orderCustomerVOList) throws Exception {
      orderCustomerDubboService.removeByListVo(orderCustomerVOList);
      return ResponseUtil.ok("success");
   }

   @GetMapping("/loadDetail")
   @ApiOperation(value = "读取零售订单详情")
   public ApiResult loadDetail(@RequestParam String orderNo) {
      return ResponseUtil.ok(orderCustomerDubboService.load(orderNo));
   }

   @PostMapping("/updateBatch")
   @ApiOperation(value = "批量更新")
   public ApiResult updateByListVo(@RequestBody List<OrderCustomerVO> orderCustomerVOList) throws Exception {
      orderCustomerDubboService.updateByListVo(orderCustomerVOList);
      return ResponseUtil.ok("success");
   }

    @PostMapping("/assignCar")
    @ApiOperation(value = "零售配车")
    public ApiResult assignCar(@RequestBody OrderCustomerVO orderCustomerVO, @CurrentUser LoginInfo loginInfo)
        throws Exception {
//      if (!orderCustomerVO.getBusType().equals(1)) {
//         return ResponseUtil.fail(4000, "非大定订单,不可配车");
//      }else {
//         return orderCustomerDubboService.assignCar(orderCustomerVO, loginInfo);
//      }
        return orderCustomerDubboService.assignCar(orderCustomerVO, loginInfo);
    }

   @PostMapping("/unbindCar")
   @ApiOperation(value = "零售解绑配车")
   public ApiResult unbindCar(@RequestBody OrderCustomerVO orderCustomerVO, @CurrentUser LoginInfo loginInfo) throws Exception {
      return orderCustomerDubboService.unbindCar(orderCustomerVO, loginInfo);
   }

   @GetMapping("/convert")
   @ApiOperation(value = "零售订单转批售订单分组列表")
   public ApiResult convertGroupList(Integer pageNum, Integer pageSize, String partnerCode) throws Exception {
      return ResponseUtil.ok(orderCustomerDubboService.convertGroupList(pageNum, pageSize, partnerCode));
   }

   @GetMapping("/getCustomerNo")
   @ApiOperation(value = "获取零售单号")
   public ApiResult getCustomerNo(OrderCustomerExOrderOemSaveReqVO v) throws Exception {
      return ResponseUtil.ok(orderCustomerDubboService.getCustomerNo(v));
   }

   @PostMapping("/convertBatch")
   @ApiOperation(value = "创建零售转批售")
   public ApiResult convertBatch(@RequestBody OrderCustomerExOrderOemSaveReqVO v, @CurrentUser LoginInfo loginInfo) throws Exception {

      return orderCustomerDubboService.convertBatch(v, loginInfo);
   }

   @PostMapping("/cancelApply")
   @ApiOperation(value = "大定退订申请")
   public ApiResult cancelApply(@RequestBody OrderCustomerVO v, @CurrentUser LoginInfo loginInfo) throws Exception {
      return orderCustomerDubboService.cancelApplyPC(v, loginInfo);
   }

   @PostMapping("/cancelAudit")
   @ApiOperation(value = "大定退订审核")
   public ApiResult cancelAudit(@RequestBody OrderCustomerVO v, @CurrentUser LoginInfo loginInfo) throws Exception {

      return orderCustomerDubboService.cancelAuditAgree(v, loginInfo);
   }

   @PostMapping("/refundCar")
   @ApiOperation(value = "退车")
   public ApiResult refundCar(@RequestBody OrderCustomerVO v, @CurrentUser LoginInfo loginInfo) throws Exception {
      return orderCustomerDubboService.refundCar(v, loginInfo);
   }

   @GetMapping("/cancelPageWait")
   @ApiOperation(value = "大定退订流程待办")
   public ApiResult cancelPageWait(OrderCustomerVO v, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
      return ResponseUtil.ok(orderCustomerDubboService.cancelPageWait(v, pageNum,pageSize,loginInfo));
   }

   @PostMapping("/cancelAuditNext")
   @ApiOperation(value = "大定退订流程待办")
   public ApiResult cancelAuditNext(@RequestBody OrderCustomerVO v, @CurrentUser LoginInfo loginInfo) throws Exception {
      return orderCustomerDubboService.cancelAuditNext(v, loginInfo);
   }

   @PostMapping("/autoNext")
   @ApiOperation(value = "大定退订流程自动下一步")
   public ApiResult autoNext(@RequestBody OrderCustomerVO v, @CurrentUser LoginInfo loginInfo) throws Exception {
      return orderCustomerDubboService.autoNext(v, loginInfo);
   }


   @PostMapping("/xiaoqu2app")
   @ApiModelProperty(value="小区发送app")
   public ApiResult xiaoqu2app(@RequestBody OrderCustomerVO v,@CurrentUser LoginInfo loginInfo) throws InvocationTargetException, IllegalAccessException {
      return orderCustomerDubboService.xiaoqu2app(v,loginInfo);
   }

   @PostMapping("/app2pms")
   @ApiModelProperty(value="app2pms")
   public ApiResult app2pms(@RequestBody CancelVOAPP v,@CurrentUser LoginInfo loginInfo) throws Exception {
      return orderCustomerDubboService.cancelApplyApp(v);
   }

   @PostMapping("/pms2app")
   @ApiModelProperty(value="pms2app")
   public ApiResult pms2app(@RequestBody CancelVOAPP v,@CurrentUser LoginInfo loginInfo) throws Exception {
      return orderCustomerDubboService.cancelApplyApp(v);
   }

   @GetMapping("/loadByOrderCustomerNo")
   @ApiModelProperty(value="loadByOrderCustomerNo")
   public ApiResult loadByOrderCustomerNo(String orderCustomerNo,@CurrentUser LoginInfo loginInfo) throws Exception {
      return orderCustomerDubboService.loadByOrderCustomerNo(orderCustomerNo);
   }

   @PostMapping("/relateOrderVehicle")
   @ApiOperation(value = "关联订货单")
   public ApiResult relateOrderVehicle(@RequestBody OrderCustomerVO v, @CurrentUser LoginInfo loginInfo) throws Exception {
      orderCustomerDubboService.relateOrderVehicle(v, loginInfo);
      return ResponseUtil.ok("success");
   }

   @GetMapping("/allList")
   @ApiOperation(value = "销售顾问本月全部订单")
   public ApiResult allList(OrderCustomerVO orderCustomerVO, @CurrentUser LoginInfo loginInfo) throws Exception {
      return ResponseUtil.ok(orderCustomerDubboService.allList(orderCustomerVO, loginInfo));
   }


   @PostMapping("/receiveApp")
   @ApiOperation(value = "接收app订单")
   public ApiResult receiveApp(@RequestBody com.yunxi.hubToBiz.pojo.OrderCustomerVO appOrderCustomerVO) throws Exception {
     return orderCustomerDubboService.receiveApp2(appOrderCustomerVO);
   }

   @GetMapping("/pms2appOnly")
   @ApiOperation(value = "pms发送app测试 result 2通过 3 拒绝 isEnd")
   public ApiResult pms2appOnly(String orderCustomerNo,Integer result,boolean isEnd) throws Exception {
      return orderCustomerDubboService.pms2appOnly(orderCustomerNo,result,isEnd);
   }

   @GetMapping("/exportOrder")
   @ApiOperation(value = "导出零售订单详情数据")
   public ApiResult exportOrder(OrderCustomerVO orderCustomerVO, @CurrentUser LoginInfo loginInfo) {
      return orderCustomerDubboService.exportOrder(orderCustomerVO,loginInfo);
   }
//
//   @GetMapping("/isDelivery")
//   @ApiOperation(value = "根据零售单号查询是否交车")
//   public ApiResult isDelivery(String orderCustomerNo, @CurrentUser LoginInfo loginInfo) throws Exception {
//      return ResponseUtil.ok(orderCustomerDubboService.isDelivery(orderCustomerNo));
//   }
//
//   @GetMapping("/isActualSales")
//   @ApiOperation(value = "根据零售单号查询是否实销")
//   public ApiResult isActualSales(String orderCustomerNo, @CurrentUser LoginInfo loginInfo) throws Exception {
//      return ResponseUtil.ok(orderCustomerDubboService.isActualSales(orderCustomerNo));
//   }
}
